<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java并发编程：无锁顺序处理方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .method-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            font-weight: bold;
            font-size: 1.2rem;
            flex-shrink: 0;
        }
        
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            font-size: 0.9rem;
            line-height: 1.6;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.1rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-size: 3rem;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1rem 1.5rem;
            border-radius: 12px;
            margin: 1rem 0;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Java并发编程的艺术
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    五种无锁方案实现线程顺序处理
                </p>
                <div class="flex justify-center space-x-4 text-lg">
                    <span><i class="fas fa-code mr-2"></i>高性能</span>
                    <span><i class="fas fa-shield-alt mr-2"></i>线程安全</span>
                    <span><i class="fas fa-sync mr-2"></i>顺序保证</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>五个线程并发处理任务的场景下，如果不使用锁，但仍然需要确保任务按顺序一个一个地处理，可以考虑以下几种方法。这些方案充分利用了Java并发包中的高级工具，既保证了线程安全，又避免了传统锁带来的性能开销。
            </p>
        </div>

        <!-- Visualization -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center text-gray-800">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>并发控制方案概览
            </h2>
            <div class="mermaid">
                graph TD
                    A[并发顺序处理需求] --> B{选择方案}
                    B --> C[Semaphore<br/>信号量控制]
                    B --> D[AtomicInteger<br/>原子操作]
                    B --> E[Exchanger<br/>线程交换]
                    B --> F[BlockingQueue<br/>阻塞队列]
                    B --> G[Condition<br/>条件变量]
                    
                    C --> H[限制并发数为1]
                    D --> I[原子性索引匹配]
                    E --> J[线程间数据交换]
                    F --> K[生产者消费者模式]
                    G --> L[等待通知机制]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Method 1: Semaphore -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="flex items-start mb-6">
                <div class="method-number mr-4">1</div>
                <div class="flex-1">
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        使用 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">Semaphore</code>（信号量）
                    </h3>
                    <p class="text-gray-600">通过限制许可数量实现顺序控制</p>
                </div>
            </div>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                <code class="bg-gray-100 text-gray-700 px-2 py-1 rounded text-sm">Semaphore</code> 可以限制同时访问某个资源的线程数量。通过控制许可的数量，可以让线程按照顺序处理任务。
            </p>

            <div class="bg-purple-50 border-l-4 border-purple-500 p-4 mb-6">
                <h4 class="font-bold text-purple-800 mb-2">
                    <i class="fas fa-lightbulb mr-2"></i>实现方式
                </h4>
                <ul class="space-y-2 text-gray-700">
                    <li><i class="fas fa-check-circle text-purple-500 mr-2"></i>初始化一个 <code class="bg-white px-1 rounded text-sm">Semaphore</code> 对象，设定许可数为1</li>
                    <li><i class="fas fa-check-circle text-purple-500 mr-2"></i>每个线程在处理任务前请求许可，处理完成后释放许可</li>
                </ul>
            </div>

            <pre><code>import java.util.concurrent.Semaphore;

public class SequentialTaskProcessor {
    private static final Semaphore semaphore = new Semaphore(1);

    public static void main(String[] args) {
        Runnable task = () -> {
            try {
                semaphore.acquire(); // 请求许可
                // 处理任务
                System.out.println(Thread.currentThread().getName() + " is processing the task");
                Thread.sleep(1000); // 模拟任务处理时间
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                semaphore.release(); // 释放许可
            }
        };

        for (int i = 0; i < 5; i++) {
            new Thread(task, "Thread-" + i).start();
        }
    }
}</code></pre>
        </div>

        <!-- Method 2: AtomicInteger -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="flex items-start mb-6">
                <div class="method-number mr-4">2</div>
                <div class="flex-1">
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        使用 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">AtomicInteger</code>（原子整型）
                    </h3>
                    <p class="text-gray-600">利用原子操作管理执行顺序</p>
                </div>
            </div>
            
            <p class="text-gray-700 mb-6 leading-relaxed">
                利用 <code class="bg-gray-100 text-gray-700 px-2 py-1 rounded text-sm">AtomicInteger</code> 来管理任务的执行顺序，通过线程的 <code class="bg-gray-100 text-gray-700 px-2 py-1 rounded text-sm">id</code> 来确保任务的顺序执行。
            </p>

            <div class="bg-purple-50 border-l-4 border-purple-500 p-4 mb-6">
                <h4 class="font-bold text-purple-800 mb-2">
                    <i class="fas fa-lightbulb mr-2"></i>实现方式
                </h4>
                <ul class="space-y-2 text-gray-700">
                    <li><i class="fas fa-check-circle text-purple-500 mr-2"></i>使用 <code class="bg-white px-1 rounded text-sm">AtomicInteger</code> 记录当前正在处理的任务的索引</li>
                    <li><i class="fas fa-check-circle text-purple-500 mr-2"></i>每个线程在处理任务前检查自己的 <code class="bg-white px-1 rounded text-sm">id</code> 是否匹配当前索引</li>
                </ul>
            </div>

            <pre><code>import java.util.concurrent.atomic.AtomicInteger;

public class SequentialTaskProcessor {
    private static final AtomicInteger currentIndex = new AtomicInteger(0);
    private static final int TOTAL_TASKS = 5;

    public static void main(String[] args) {
        Runnable task = () -> {
            try {
                int threadId = Integer.parseInt(Thread.currentThread().getName().split("-")[1]);
                while (true) {
                    if (currentIndex.get() == threadId) {
                        // 处理任务
                        System.out.println(Thread.currentThread().getName() + " is processing the task");
                        Thread.sleep(1000); // 模拟任务处理时间
                        currentIndex.incrementAndGet(); // 更新索引
                        break;
                    }
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        };

        for (int i = 0; i < TOTAL_TASKS; i++) {
            new Thread(task, "Thread-" + i).start();
        }
    }
}</code></pre>
        </div>

        <!-- Method 3: Exchanger -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class